import 'package:image_picker_plus_nic/image_picker_plus_nic.dart';
import 'package:image_picker_plus_nic/src/utilities/enum.dart';
import 'package:flutter/material.dart';

import 'dialog_widget.dart';

class ImagePickerPlus {
  final BuildContext _context;
  const ImagePickerPlus(this._context);

  // 選擇單張圖片
  Future<SelectedImagesDetails?> pickImage({
    required ImageSource source,
    required Color primaryColor,
    required TextStyle textStyle,
    UiTexts? uiText,
  
  }) async {
    GalleryDisplaySettings galleryDisplaySettings;
    if (uiText != null) {
      galleryDisplaySettings = GalleryDisplaySettings(uiTexts: uiText);
    } else {
      galleryDisplaySettings = const GalleryDisplaySettings();
    }
    return _openCustomPicker(
      primaryColor: primaryColor,
      textStyle: textStyle,
      pickerSource: PickerSource.image,
      galleryDisplaySettings: galleryDisplaySettings,
      source: source,
    );
  }

  // 多擇圖片
  Future<SelectedImagesDetails?> pickMultiImage({
    required ImageSource source,
    required Color primaryColor,
    required TextStyle textStyle,
    UiTexts? uiText,
    int maximumSelection = 5,
  }) async {
    GalleryDisplaySettings galleryDisplaySettings;
    if (uiText != null) {
      galleryDisplaySettings = GalleryDisplaySettings(
          uiTexts: uiText, maximumSelection: maximumSelection);
    } else {
      galleryDisplaySettings =
          GalleryDisplaySettings(maximumSelection: maximumSelection);
    }
    return _openCustomPicker(
      primaryColor: primaryColor,
      textStyle: textStyle,
      pickerSource: PickerSource.image,
      galleryDisplaySettings: galleryDisplaySettings,
      source: source,
      multiSelection: true,
    );
  }

  // 多擇且預覽圖片
  Future<SelectedImagesDetails?> pickAndPreviewMultiImage({
    required ImageSource source,
    required Color primaryColor,
    required TextStyle textStyle,
    UiTexts? uiText,
    int maximumSelection = 5,
  }) async {
    GalleryDisplaySettings galleryDisplaySettings;
    if (uiText != null) {
      galleryDisplaySettings = GalleryDisplaySettings(
        showImagePreview: true,
        uiTexts: uiText,
        maximumSelection: maximumSelection,
      );
    } else {
      galleryDisplaySettings = GalleryDisplaySettings(
        showImagePreview: true,
        maximumSelection: maximumSelection,
      );
    }
    return _openCustomPicker(
      primaryColor: primaryColor,
      textStyle: textStyle,
      pickerSource: PickerSource.image,
      galleryDisplaySettings: galleryDisplaySettings,
      source: source,
      multiSelection: true,
    );
  }

  // 多選擇並且裁切圖片
  Future<SelectedImagesDetails?> pickAndCropImage({
    required ImageSource source,
    required Color primaryColor,
    required TextStyle textStyle,
    UiTexts? uiText,
    int? selectIndex,
  }) async {
    GalleryDisplaySettings galleryDisplaySettings;
    if (uiText != null) {
      galleryDisplaySettings =
          GalleryDisplaySettings(cropImage: true, uiTexts: uiText);
    } else {
      galleryDisplaySettings = const GalleryDisplaySettings(cropImage: true);
    }
    return _openCustomPicker(
      primaryColor: primaryColor,
      textStyle: textStyle,
      galleryDisplaySettings: galleryDisplaySettings,
      pickerSource: PickerSource.image,
      source: source,
      selectIndex: selectIndex,
    );
  }

  Future<SelectedImagesDetails?> _openCustomPicker({
    required ImageSource source,
    required Color primaryColor,
    required TextStyle textStyle,
    GalleryDisplaySettings galleryDisplaySettings =
        const GalleryDisplaySettings(),
    bool multiSelection = false,
    required PickerSource pickerSource,
    int? selectIndex,
  }) async {
    return await showModalBottomSheet(
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(15)),
      ),
      isDismissible: true,
      isScrollControlled: true,
      enableDrag: false,
      context: _context,
      builder: (context) {
        return DialogWidget(
          primaryColor: primaryColor,
          textStyle: textStyle,
          galleryDisplaySettings: galleryDisplaySettings,
          pickerSource: pickerSource,
          source: source,
          heightFactor: 0.9,
          multiSelection: multiSelection,
          onCancel: onCancel,
        );
      },
    );
  }

  // 取消
  void onCancel() {
    Navigator.of(_context).maybePop(null);
  }
}
